

using System;
using UnityEngine;

//这个服务 管理的是PhysicsConfig 的执行和管理
public class PhysicsService : FSMServiceiBase
{
    public override void Init(FSM fSM)
    {
        base.Init(fSM);
    }

    public override void OnAnimationEnd(PlayerState state)
    {
        base.OnAnimationEnd(state);
        ReSetAllExcuted();
        Stop();
    }

    public override void OnBegin(PlayerState state)
    {
        base.OnBegin(state);
        ReSetAllExcuted();
    }


    public override void OnDisable(PlayerState state)
    {
        base.OnDisable(state);
    }

    public override void OnEnd(PlayerState state)
    {
        base.OnEnd(state);
        ReSetAllExcuted();
        Stop();
    }

    public override void OnUpdate(float normalizedTime, PlayerState state)
    {
        base.OnUpdate(normalizedTime, state);
        var e = state.stateEntity.physicsConfig;
        if(e != null && e.Count>0)
        {
            
            for(int i = 0; i < e.Count; i++)
            {
                var config = e[i];
                if(normalizedTime >= config.trigger && GetExecuted(i) ==false)
                {
                    Do(config, state);
                    SetExcuted(i);
                }
            }
        }

        if (begin)
        {
            if (normalizedTime <= currentEntity.time)
            {
                if (currentEntity.time > 0)
                {
                    //进度
                    var process = (normalizedTime - currentEntity.trigger)/(currentEntity.time-currentEntity.trigger);
                    float lerpTime = currentEntity.cure.Evaluate(process);
                    //插值
                    var speed = Vector3.Lerp(Vector3.zero,force, lerpTime);
                    player.AddForce(speed,currentEntity.ignore_gravity);

                    if (currentEntity.stop_dst > 0)
                    {
                        var begin = player._transform.position + Vector3.up;
                        var result = Physics.Linecast(begin, begin + player._transform.forward * currentEntity.stop_dst, player.GetEnemyLayerMask());
                        if (result)
                        {
                            Stop();
                        }
                    }
                }
            }
            else
            {
                
                Stop(); 
            }

        }
    }

    private void Stop()
    {

        begin = false;
        player.RemoveForce();
        //if (begin)
        //{
        //    begin = false;
        //    player.RemoveForce();
        //}
    }

    bool begin = false;
    PhysicsConfig currentEntity;
    Vector3 force;
    /// <summary>
    /// 到了触发点
    /// </summary>
    public void Do(PhysicsConfig physicsConfig,PlayerState state)
    {
        //执行这个配置的所需时间
        float t = state.clipLength * ((physicsConfig.time - physicsConfig.trigger)/1);
        if(t<=0)
        {
            begin = false;
        }
        else
        {
            currentEntity = physicsConfig;
            if(physicsConfig.time > 0)
            {
                force = currentEntity.force/t;
            }
            else
            {
                force = currentEntity.force;
            }
            begin = true;
        }
    }


    public override void ReLoop(PlayerState state)
    {
        base.ReLoop(state);
    }

    public override void ReStart(PlayerState state)
    {
        base.ReStart(state);
        ReSetAllExcuted();
    }
}